﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Cartoon.Config;
using Cartoon.Skin;

namespace CartoonViewer
{
    

    /// <summary>
    /// W_Setting.xaml 的交互逻辑
    /// </summary>
    public partial class W_Setting : Window
    {

        protected bool IsChanged = false;
        ConfigForCartoon config;
        public event EventHandler Saved;
        ISkinService SkinS;
        public W_Setting()
        {
            InitializeComponent();
            this.SetChanged(false);
            this.TxtSavePath.IsEnabled = false;
            config = ConfigForCartoon.CreateInstance();
            this.TxtSavePath.Text = config.SavePath;
        }

        public W_Setting(ISkinService _SkinS):this()
        {
            this.SkinS = _SkinS;
            this.SkinS.Changed += new Action(SkinS_Changed);
            this.LoadBackGround();
        }

        void SkinS_Changed()
        {
            this.LoadBackGround();
        } 

        private void LoadBackGround()
        {
            ImageBrush b = new ImageBrush();
            b.ImageSource = this.SkinS.Skin.BackGround;
            this.Win.Background = b;  
        }

        private void BtnYes_Click(object sender, RoutedEventArgs e)
        {
            if (this.IsChanged == true)
            {
                this.Save();
            }

            this.Close();
        }

        private void BtnNo_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            if (this.IsChanged == true)
            {
                this.Save();
                
            }
        }

        private void BtnBrowse_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dgFolder = new System.Windows.Forms.FolderBrowserDialog();
            if (dgFolder.ShowDialog() ==  System.Windows.Forms.DialogResult.OK)
            {
                string oldpath = this.TxtSavePath.Text;
                string newpath =  dgFolder.SelectedPath;
                if (string.IsNullOrEmpty(newpath) == false)
                {
                    if (newpath.EndsWith("\\") == false)
                    {
                        newpath += "\\";
                    }

                    if (newpath != oldpath)
                    { 
                        this.TxtSavePath.Text = newpath;
                        this.SetChanged(true);
                    }
                }
            }
        }

        protected void SetChanged(bool changed)
        {
            this.IsChanged = changed; 
            this.BtnSave.IsEnabled = this.IsChanged;
        }

        protected void Save()
        {
            if (this.IsChanged == true)
            {
                this.config.SavePath = this.TxtSavePath.Text;

                this.OnSaved();
            }

            this.SetChanged(false);
        }

        protected void OnSaved()
        {
            if (this.Saved != null)
            {
                this.Saved(this, null);
            }
        }

        private void LbMinWin_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {


            
        }

     

        bool IsWinMoving = false;
        Point pbegin;
        Point pend;
        private void CvsTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.WinBtnClose.IsMouseOver || this.WinBtnMin.IsMouseOver || this.CvsTitle.IsMouseOver == false) return;



            this.pbegin = e.GetPosition(this);
            this.IsWinMoving = this.CvsTitle.CaptureMouse();
            this.IsWinMoving = true;
            
        }

        private void CvsTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.IsWinMoving == false) return;

            this.pend = e.GetPosition(this);

            if (pbegin.X == pend.X && pbegin.Y == pend.Y) return;

            double moveX, moveY;

            moveX = pend.X - pbegin.X;
            moveY = pend.Y - pbegin.Y;

            this.Left += moveX;
            this.Top += moveY;

        }

        private void CvsTitle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
           
            this.IsWinMoving = false;
            this.CvsTitle.ReleaseMouseCapture();

        }

      

        private void WinBtnClose_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	this.Close(); 
        }

        private void WinBtnMin_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	this.WindowState = WindowState.Minimized;
        }

       
    }
}
